﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools;
using 纺织贸易管理系统;

namespace BLL
{
    public class 销售预出库BLL
    {
        /// <summary>
        /// 检查是否已经预出库了
        /// </summary>
        /// <param name="Details"></param>
        /// <returns></returns>
        public static string CheckIsPreShipments(List<danjumingxitable> Details)
        { 
            foreach (var m in Details)
            {
                var yanse = m.yanse.Split('/')[0];
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == m.rkdh
                && x.pingming == m.pingming
                && x.yanse == yanse 
                &&x.OrderNum==m.OrderNum
                && x.ganghao == m.ganghao);
                if (ruku != null)
                {
                   if( ruku.PreSale == true)
                    {
                        return ruku.ganghao ;
                    }                  
                }
            }
          return string.Empty;
        }
        /// <summary>
        /// 检查是否已经预出库了
        /// </summary>
        /// <param name="Details"></param>
        /// <returns></returns>
        public static bool CheckIsPreShipments(danjumingxitable item)
        {
                var yanse = item.yanse.Split('/')[0];
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == item.rkdh && x.pingming == item.pingming && x.yanse == yanse && x.ganghao == item.ganghao);
                return ruku != null?ruku.PreSale : false;
        }
        public static void 保存单据(BLLParams parameter)
        {
            if (parameter.Rolls.Count == 0)
            {
                var ganghao = 销售预出库BLL.CheckIsPreShipments(parameter.Details);
                if (!string.IsNullOrEmpty(ganghao))
                {
                    throw new Exception($"缸号:{ganghao}\r\n已经预出库了！不能二次开单");
                }
            }
            DanjuTableService.InsertDanjuTable(parameter.Danju);
            danjumingxitableService.Insertdanjumingxitablelst(parameter.Details );
            var fahuodans=JuanHaoTableService.ConvertToFahuodan(parameter.Rolls );
            foreach (var item in fahuodans)
            {
                item.rq = parameter.Danju.rq;
                item.dh = parameter.Danju.dh;
            }
            if(Connect.dbcontext.Queryable<FahuoDan>().Where(x=>parameter.Rolls.Select(y=>y.JuanHao).ToList().Contains(x.jh)).Any())
            {
                throw new Exception($"存在已经预出库的卷了！不能二次开单");
            }
            FahuoDanService.InsertFahuoDan(fahuodans);
            审核(parameter.Details);
        }
        public static void 修改单据(BLLParams para)
        {
           删除单据(para.Danju.dh);
           保存单据(para);
        }
        public static void 删除单据(string DocNum)
        {
            反审核(Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao ==DocNum ).ToList());
            DanjuTableService.DeleteDanjuTable(x=>x.dh==DocNum);
            danjumingxitableService.Deletedanjumingxitable(x=>x.danhao==DocNum);
            FahuoDanService.DeleteFahuoDan(x=>x.dh==DocNum);
        }
        private static void 审核(List<danjumingxitable > Details)
        {
            var Storages = new List<danjumingxitable>();
            foreach (var m in Details)
            {
                var yanse = m.yanse.Split('/')[0];
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == m.rkdh 
                && x.pingming == m.pingming
                && x.yanse == yanse
                && x.ganghao == m.ganghao);
                if (ruku != null)
                {
                    ruku.PreSale = true;
                    ruku.PreSaleQuantity += m.toupimishu;
                    ruku.PreSaleRolls+= m.chengpingjuanshu;
                    ruku.PreThumbnail = ruku.UpRolledNum -ruku.PreSaleQuantity;
                    Storages.Add(ruku);
                }
            }
            danjumingxitableService.Updatedanjumingxitable (Storages);
        }
        private  static void 反审核(List<danjumingxitable> Details)
        {
            var Storages = new List<danjumingxitable>();
            foreach (var m in Details)
            {
                var yanse = m.yanse.Split('/')[0];
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == m.rkdh
                && x.pingming == m.pingming
                && x.yanse == yanse
                && x.OrderNum == m.OrderNum
                && x.ganghao == m.ganghao);
                if (ruku != null)
                {
                    ruku.PreSale = false;
                    ruku.PreSaleQuantity -= m.toupimishu;
                    ruku.PreSaleRolls = 0;
                    ruku.PreThumbnail = ruku.UpRolledNum -ruku.PreSaleQuantity;
                    Storages.Add(ruku);
                }
            }
            Connect.dbcontext.Deleteable<FahuoDan>().Where(x => x.dh == Details[0].danhao).ExecuteCommand();
            danjumingxitableService.Updatedanjumingxitable(Storages);
        }
    }
}
